﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Web.Security;
using System.Net;

public partial class Account_NewTeam : System.Web.UI.Page
{
    User user;
    JsonParser jsonParser;
    UserTeam userTeam;

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Page_Init(object sender, EventArgs e)
    {

        jsonParser = new JsonParser();
        user = (User)Session["UserSession"];        
        Createteam_btn.Click += new EventHandler(this.createTeam_Click);

        try
        {
            UserTeam team = jsonParser.getUserTeam(user.username);
            if (team != null)
            {
                Response.Redirect("~/Account/Profile.aspx");
            }
            
        }
        catch (Exception ex)
        {
           
        }
        
    }

    protected void createTeam_Click(Object sender, EventArgs e)
    {
        
        if (user != null)
        {
            string name = "";
            if (!teamname.Text.Equals(""))
            {
                name = teamname.Text;
                bool teamCreated = jsonParser.createUserTeam(user.username, user.password, name);

                if (teamCreated)
                {
                    Response.Redirect("~/Account/ManageLeague.aspx");
                }
                else
                {
                    // Feilmelding
                    team_exception.Text = "That team name is taken!";
                }
            }
            else
            {
                // Feilmelding
                team_exception.Text = "Your team needs a name!";
            }
            
        }

    }
}